package com.example.smart.adapter.home

import android.app.Activity
import android.media.MediaPlayer
import android.os.Build.VERSION_CODES.P
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.example.lib.extend.gone
import com.example.lib.extend.setOnDoubleClickListener
import com.example.lib.extend.visi
import com.example.smart.R
import com.example.smart.base.adapter.BaseRecyclerAdapter
import com.example.smart.base.adapter.BaseViewHolder
import com.example.smart.data.AiConversationData
import com.example.smart.databinding.ItemAiConversationReceiveBinding
import com.example.smart.databinding.ItemAiConversationSendBinding

class AiConversationAdapter(val activity: Activity) : BaseRecyclerAdapter<ViewBinding, AiConversationData>(activity) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<ViewBinding> {
        return if (viewType == 0) {
            BaseViewHolder(ItemAiConversationSendBinding.bind(LayoutInflater.from(context)
                .inflate(R.layout.item_ai_conversation_send, parent, false)))
        } else {
            BaseViewHolder(ItemAiConversationReceiveBinding.bind(LayoutInflater.from(context)
                .inflate(R.layout.item_ai_conversation_receive, parent, false)))
        }
    }

    override fun onBindView(itemView: ViewBinding, bean: AiConversationData, position: Int) {
        if (getItemViewType(position) == 0) {
            with(itemView as ItemAiConversationSendBinding) {
                if (bean.msgType == AiConversationData.MSG_TEXT) {
                    ItemMessage.visi()
                    ItemAudio.gone()
                    ItemTime.gone()
                    ItemMessage.text = bean.msg
                } else {
                    ItemMessage.gone()
                    ItemAudio.visi()
                    ItemTime.visi()
                    ItemTime.text = bean.time.toString()

                    ItemAudio.setOnDoubleClickListener {
                        clickListener?.click(bean, position)
                    }
                }
            }
        } else {
            with(itemView as ItemAiConversationReceiveBinding) {
                ItemCountdown.cancel()
                if (bean.loading) {
                    ItemMessage.gone()
                    ItemLoading.visi()
                    if (bean.msgType == AiConversationData.MSG_TEXT) {
                        ItemCountdown.setTime(2000)
                        ItemCountdown.setOnOverListener {
                            ItemMessage.visi()
                            ItemLoading.gone()
                            updateItem(position, bean.also {
                                it.loading = false
                            })
                            ItemMessage.text = bean.msg
                        }
                    }
                } else {
                    ItemMessage.visi()
                    ItemLoading.gone()
                    ItemMessage.text = bean.msg
                }
            }
        }
    }

    override fun getItemViewType(position: Int): Int {
        return getItemAtPosition(position).type
    }

    override fun add(data: AiConversationData, index: Int) {
        if (data.type == AiConversationData.TYPE_SEND) {
            if (isNotEmpty() && getItemAtPosition(size() - 1).type == AiConversationData.TYPE_RECEIVE && getItemAtPosition(
                        size() - 1).loading) {
                updateItem(size() - 1, getItemAtPosition(size() - 1).also {
                    it.loading = false
                    it.cancel = true
                    it.msg = "已取消"
                })
                super.add(data, index)
            } else {
                super.add(data, index)
            }
        } else {
            super.add(data, index)
        }
    }

}